require 'erb'


# === アクションの基底．
#
# Author:: krdlab
#
class Action
  attr_accessor :request

  def initialize(request)
    @request = request
  end

  def run
    # 何もしない
  end
end


# === ページを出力する．
#
# Author:: krdlab
#
class ViewAction < Action
  attr_accessor :path

  def initialize(path, request)
    super(request)
    @path = path
  end

  def run
    content = open(@path) {|f| f.read }.untaint
    view = ERB.new(content).result(binding)
    @request.cgi.out() { view }
  end
end


# === リダイレクト．
#
# Author:: krdlab
#
class RedirectAction < Action
  attr_accessor :path

  def initialize(path, request)
    super(request)
    @path = path
  end

  def run
    @equest.cgi.header({
      'status' => 'REDIRECT',
      'Location' => @path
    })
  end
end


# === エラー．
#
# Author:: krdlab
#
class ErrorAction < Action
  attr_accessor :exception

  def initialize(exception, request)
    super(request)
    @exception = exception
  end

  def run
    @request.cgi.out() { @exception.to_s }
  end
end

